CREATE procedure create_order
    @account_number varchar(30),
    @DishNumber varchar(20),
    @OrderNumber varchar(20),
    @all_address varchar(200),
    @flag int out
as
declare
    @cost decimal(11,2),
    @coin decimal(11,2),
    @OrderIsExists int,
    @haveIsExists int,
    @MerchantTel varchar(15),
    @CustomerTel varchar(15),
    @PayOnTime datetime,
    @dishNum int
    select @PayOnTime = getdate()
    select @CustomerTel = customer_phone_number from account where account_number = @account_number;
    select @cost = dish_price from dish where dish_number = @DishNumber;
    select @dishNum = dish_num from choice where dish_number = @DishNumber;--菜品数量
    set @cost = @cost * @dishNum;--
    select @coin = current_money from account where account_number = @account_number;
    select @MerchantTel = merchant_phone_number from dish where dish_number = @DishNumber
    if @cost >= @coin begin
        set @flag = 0;
        return--余额不足导致支付失败
    end
    else begin
        select @OrderIsExists = count(open_order_number)from open_order where open_order_number = @OrderNumber
        select @haveIsExists =count(dish_number)from open_order_have where dish_number = @DishNumber and open_order_number = @OrderNumber
        if @OrderIsExists = 0 begin
            --不存在该订单号，则创建新的订单
            insert into open_order
            values(
                      @OrderNumber,
                      @all_address,
                      @PayOnTime,
                      @cost,
                      0,
                      @CustomerTel,
                      @MerchantTel
                  )
        end
        else begin
            --存在该订单号，表示该订单包含多个菜品，则需往其中添加信息
            update open_order
            set whole_price = whole_price + @cost
            where open_order_number = @OrderNumber
        end
        if @haveIsExists = 0 begin
            --不存在该含有关系，则创建一个含有关系
            insert into open_order_have
            values(
                      @OrderNumber,
                      @DishNumber,
                      @dishNum
                  )
        end
        --         else begin
--             --已存在该含有关系，则数量+1
--             update open_order_have
--             set whole_num = whole_num + 1
--             where open_order_number = @OrderNumber and dish_number = @DishNumber
--         end
        -- 从余额中扣减费用
        update account set current_money = @coin - @cost where account_number = @account_number
        set @flag = 1;
        return
    end
go

